package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IDispatchEngineeringInstallationOrderView;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.IView.ISendPublicationOrderView;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.UserInfoBean;
import com.cloudsigns.factorymanager2.engineering.presenter.DispatchEngineeringInstallationOrderPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.SendPublicationOrderPresenter;
import com.cloudsigns.factorymanager2.project.iview.ISurveyOrderSendForManagerView;
import com.cloudsigns.factorymanager2.project.presenter.SurveyOrderSendForManagerPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

/**
 * 工程 工单管理 工单分派 工程安装
 * Created by tony on 2018/2/2.
 */

public class DispatchEngineeringInstallationOrderActivity extends Activity implements View.OnClickListener, IDispatchEngineeringInstallationOrderView, IEngineeringInstallOrderDetailView,
        ISendPublicationOrderView, ISurveyOrderSendForManagerView {
    private View rl_back;
    private TextView tv_title;
    private ImageView img_edit_dispatch_information;
    private TextView tv_project_name, tv_engineering_name;
    private TextView edt_dispatching_way_colon, edt_receive_colon, edt_external_dispatch_people, edt_external_dispatch_time;//edt_install_docking_colon;
    private LinearLayout ll_dispatch_content;
    private TextView btn_confirm_the_dispatch, btn_confirm_detele;
    private View rl_engineering_name;
    private final String TAG = DispatchEngineeringInstallationOrderActivity.class.getSimpleName();
    private boolean isClick = false;
    private DispatchEngineeringInstallationOrderPresenter dispatchEngineeringInstallationOrderPresenter;
    private OrderParentBean orderParentBean;
    private UserInfoBean userInfoBean;
    private EngineeringInstallOrderDetailPresenter engineeringInstallOrderDetailPresenter;
    private SendPublicationOrderPresenter sendPublicationOrderPresenter;
    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    private SurveyOrderSendForManagerPresenter surveyOrderSendForManagerPresenter;
    private String from;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_dispatch_engineering_installation_order);

        dispatchEngineeringInstallationOrderPresenter = new DispatchEngineeringInstallationOrderPresenter(this);
        engineeringInstallOrderDetailPresenter = new EngineeringInstallOrderDetailPresenter(this);
        sendPublicationOrderPresenter = new SendPublicationOrderPresenter(this);
        surveyOrderSendForManagerPresenter = new SurveyOrderSendForManagerPresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            from = mIntent.getStringExtra(ConstantUtil.KEY_1);
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
        }

        if (null != orderParentBean) {
            CommonEnum.EngineeringORderCategory engineeringORderCategory = orderParentBean.getCategory();
            if (null != engineeringORderCategory) {
                if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.INSTALL) {
                    engineeringInstallOrderDetailPresenter.getEngineeringInstallOrderDetail();
                }
            }
        }


        initData();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        img_edit_dispatch_information = findViewById(R.id.img_edit_dispatch_information);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        edt_dispatching_way_colon = findViewById(R.id.edt_dispatching_way_colon);
        edt_receive_colon = findViewById(R.id.edt_receive_colon);
        // edt_install_docking_colon = findViewById(R.id.edt_install_docking_colon);
        edt_external_dispatch_people = findViewById(R.id.edt_external_dispatch_people);
        edt_external_dispatch_time = findViewById(R.id.edt_external_dispatch_time);
        ll_dispatch_content = findViewById(R.id.ll_dispatch_content);
        btn_confirm_the_dispatch = findViewById(R.id.btn_confirm_the_dispatch);
        btn_confirm_detele = findViewById(R.id.btn_confirm_detele);
        rl_engineering_name = findViewById(R.id.rl_engineering_name);

        rl_back.setOnClickListener(this);
        img_edit_dispatch_information.setOnClickListener(this);
        btn_confirm_the_dispatch.setOnClickListener(this);
        btn_confirm_detele.setOnClickListener(this);
        rl_engineering_name.setOnClickListener(this);

    }

    private void initData() {
        if (null != orderParentBean) {
            tv_project_name.setText(orderParentBean.getProjectName());
            tv_engineering_name.setText("/" + orderParentBean.getEngineeringName());

            CommonEnum.EngineeringORderCategory engineeringORderCategory = orderParentBean.getCategory();
            if (null != engineeringORderCategory) {
                if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.INSTALL) {
                    tv_title.setText(getResources().getString(R.string.engineering_installation_order));
                } else if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                    tv_title.setText(getResources().getString(R.string.publication));
                } else if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.SURVEY) {
                    tv_title.setText(getResources().getString(R.string.engineering_patro));
                }
            }

        }

        if (null != engineeringInstallationOrderBean) {
            CommonEnum.AssignStatus assignStatus = engineeringInstallationOrderBean.getAssignStatus();
            if (null != assignStatus) {
                if (assignStatus == CommonEnum.AssignStatus.CREATED || assignStatus == CommonEnum.AssignStatus.EDITED) {

                } else {
                    edt_dispatching_way_colon.setText("内派");

                    edt_receive_colon.setText(engineeringInstallationOrderBean.getAssignReceiveAccountName() );

                    edt_external_dispatch_people.setText(engineeringInstallationOrderBean.getCreateOrderAccountName());
                    edt_external_dispatch_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getSendOrderTime()));


                    ll_dispatch_content.setVisibility(View.VISIBLE);
                    isClick = false;
                    setClickable();

                }
            }
        }

    }

    private void setClickable() {
        if (isClick) {
            btn_confirm_the_dispatch.setBackgroundResource(R.mipmap.btn_long_bg_green);
        } else {
            btn_confirm_the_dispatch.setBackgroundResource(R.mipmap.btn_long_bg_un);
        }
    }

    private void dispatchEngineeringOrder() {
        CommonEnum.EngineeringORderCategory engineeringORderCategory = orderParentBean.getCategory();
        if (null != engineeringORderCategory) {
            if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.INSTALL) {
                dispatchEngineeringInstallationOrderPresenter.dispatchEngineeringInstallationOrder();

            } else if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                sendPublicationOrderPresenter.sendPublicationOrder();
            } else if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.SURVEY) {
                surveyOrderSendForManagerPresenter.surveyOrderSendForManager();
            }
        }
    }

    private void engineeringOrderDetail() {
        Intent mIntent;
        CommonEnum.EngineeringORderCategory engineeringORderCategory = orderParentBean.getCategory();
        if (null != engineeringORderCategory) {
            // 工程安装
            if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.INSTALL) {
                mIntent = new Intent(DispatchEngineeringInstallationOrderActivity.this, ViewEngineeringInstallOrderDetailActivity.class);
                // mIntent = new Intent(DispatchEngineeringInstallationOrderActivity.this, DispatchEditEngineeringInstallOrderActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean);
                startActivity(mIntent);
            }
            // 广告上刊
            else if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                // mIntent = new Intent(DispatchEngineeringInstallationOrderActivity.this, PublicationOrderDetailActivity.class);
                mIntent = new Intent(DispatchEngineeringInstallationOrderActivity.this, EngineeringPublicationOrderHasBeenReceivedActicity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                mIntent.putExtra(ConstantUtil.KEY_2, orderParentBean);
                startActivity(mIntent);
            }
            // 现场勘查
            else if (engineeringORderCategory == CommonEnum.EngineeringORderCategory.SURVEY) {
                mIntent = new Intent(DispatchEngineeringInstallationOrderActivity.this, EngineeringSurveyOrderHasBeenReceivedActicity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                mIntent.putExtra(ConstantUtil.KEY_2, orderParentBean);
                startActivity(mIntent);
            }
        }


    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 派工信息
            case R.id.img_edit_dispatch_information:
                mIntent = new Intent(DispatchEngineeringInstallationOrderActivity.this, EditDispatchInformationForEngineeringInstallationOrderActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, CommonEnum.ROLECATEGORY.TEAM);
                mIntent.putExtra(ConstantUtil.KEY_2, orderParentBean);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            // 删除工单
            case R.id.btn_confirm_detele:
                if (isClick) {
                    ll_dispatch_content.setVisibility(View.GONE);
                    Toast.makeText(DispatchEngineeringInstallationOrderActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    isClick = false;
                    setClickable();
                }
                break;
            // 确认派工
            case R.id.btn_confirm_the_dispatch:
                if (isClick)
                    dispatchEngineeringOrder();
                break;
            // 产品名称/项目名称/工程名称
            case R.id.rl_engineering_name:
                engineeringOrderDetail();
                break;


        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    String dispatch_way = data.getStringExtra("dispatch_way");

                    edt_dispatching_way_colon.setText(dispatch_way);

                    if (dispatch_way.equals("内派")) {
                        userInfoBean = (UserInfoBean) data.getSerializableExtra(ConstantUtil.KEY_1);

                    } else if (dispatch_way.equals("外派")) {

                    }


                    if (null != userInfoBean) {
                        edt_receive_colon.setText(userInfoBean.getName());
                        if (null != engineeringInstallationOrderBean) {
                            edt_external_dispatch_people.setText(engineeringInstallationOrderBean.getCreateOrderAccountName());
                            edt_external_dispatch_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getSendOrderTime()));
                        }


                        ll_dispatch_content.setVisibility(View.VISIBLE);
                        isClick = true;
                        setClickable();
                    }
                    // edt_install_docking_colon.setText(userInfoBean.getName() + "：" + userInfoBean.getId());
//                    edt_external_dispatch_company.setText(data.getStringExtra(ConstantUtil.KEY_2));


                }

                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public String getId() {
        if (null != orderParentBean)
            return orderParentBean.getId();

        return "";
    }

    @Override
    public String getReceiveAccountId() {
        if (null != userInfoBean)
            return userInfoBean.getId();
        return "";
    }

    @Override
    public void surveyOrderSendForManagerSuccess() {
        Toast.makeText(DispatchEngineeringInstallationOrderActivity.this, "派工成功", Toast.LENGTH_SHORT).show();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, new Intent());
        finish();
    }

    @Override
    public void surveyOrderSendForManagerFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void sendPublicationOrderSuccess() {
        Toast.makeText(DispatchEngineeringInstallationOrderActivity.this, "派工成功", Toast.LENGTH_SHORT).show();
        setResult(6, new Intent());
        finish();
    }

    @Override
    public void sendPublicationOrderFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void diapatchSuccess() {
        Toast.makeText(DispatchEngineeringInstallationOrderActivity.this, "派工成功", Toast.LENGTH_SHORT).show();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, new Intent());
        finish();
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public long getFeedbackDate() {
        return 0;
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return null;
    }

    @Override
    public void getEngineeringInstallOrderDetailSuccessed(EngineeringInstallationOrderBean engineeringInstallationOrderBean) {
        this.engineeringInstallationOrderBean = engineeringInstallationOrderBean;
        initData();
    }

    @Override
    public void getEngineeringInstallOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
